package com.contactbook.web.client;

import java.util.Set;

import com.contactbook.web.client.dto.ContactDTO;
import com.contactbook.web.client.dto.GroupDTO;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MyAccountWidget extends View {
	
	private class ClickDeleteAccountHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent arg0) {
			contactBook.switchView(Constants.DeleteAccountViewName);
		}		
	}

	private static final String TITLE = "My Account informations";
	
	private VerticalPanel content;
	
	private VerticalPanel panelWidgets;
	
	private Label labelNbGroups = new Label("");
	private Label labelNbContacts = new Label("");
	
	public MyAccountWidget(ContactBook contactBook) {
		super(contactBook);
		panelWidgets = new VerticalPanel();
	}
	
	@Override
	public VerticalPanel getContent() {
		if (contactBook.isLogged()) {
			clearContent();
			buildView();
			return content;
		} else
			contactBook.switchView(Constants.LoginViewName);
		return null;
	}

	@Override
	public String getTitle() {
		return TITLE;
	}

	private VerticalPanel buildView() {
		content = new VerticalPanel();
		
		DecoratorPanel panelMyAccount = new DecoratorPanel();
		
		panelWidgets.setSpacing(5);
		panelWidgets.setWidth(Constants.SmallPanelWidgetsStandartWidth);
		
		getAccountInformation();
		
		panelMyAccount.add(panelWidgets);
		content.add(panelMyAccount);
		return content;
	}
	
	private void getAccountInformation() {
		
		HorizontalPanel panelAccountHeader = new HorizontalPanel();
		
		Label labelAccount = new Label("My account  ");
		labelAccount.setStyleName("gwt-label-labelnote");
		
		Image deleteAccountIcon = new Image(Constants.IconDeletePath);
		deleteAccountIcon.setSize(Constants.IconSizeConstant, Constants.IconSizeConstant);
		deleteAccountIcon.setTitle("Delete your account");
		deleteAccountIcon.addClickHandler(new ClickDeleteAccountHandler());
		
		panelAccountHeader.add(labelAccount);
		panelAccountHeader.add(deleteAccountIcon);
		
		panelWidgets.add(panelAccountHeader);
		
		FlexTable tableAccount = new FlexTable();
		
		Label labelLogin = new Label("Login :");
		labelLogin.setWidth("90");
		Image editLoginIcon = new Image(Constants.IconEditPath);
		editLoginIcon.setSize(Constants.IconSizeConstant, Constants.IconSizeConstant);
		
		Label labelPassword = new Label("Password :");
		labelPassword.setWidth("90");
		
		Image editPasswordIcon = new Image(Constants.IconEditPath);
		editPasswordIcon.setSize(Constants.IconSizeConstant, Constants.IconSizeConstant);
		
		Label labelMyLogin = new Label(contactBook.getCurrentSession().getLogin());
		Label labelMyPassword = new Label(contactBook.getCurrentSession().getPassword());
		
		tableAccount.setWidget(0, 0, labelLogin);
		tableAccount.setWidget(0, 1, labelMyLogin);
		tableAccount.setWidget(0, 2, editLoginIcon);
		
		tableAccount.setWidget(1, 0, labelPassword);
		tableAccount.setWidget(1, 1, labelMyPassword);
		tableAccount.setWidget(1, 2, editPasswordIcon);
		
		panelWidgets.add(tableAccount);
		
		// Statistics
		Label labelStatistics = new Label("My statistics");
		labelStatistics.setStyleName("gwt-label-labelnote");
		labelStatistics.setHeight("50");
		
		panelWidgets.add(labelStatistics);		

		panelWidgets.add(labelNbGroups);
		panelWidgets.add(labelNbContacts);
		
		// Groups
		AsyncCallback<Set<GroupDTO>> callbackGroups = new AsyncCallback<Set<GroupDTO>>() {

			@Override
			public void onFailure(Throwable arg0) {				
			}

			@Override
			public void onSuccess(Set<GroupDTO> result) {
				//nbGroups = result.size();
				labelNbGroups.setText("You have " + String.valueOf(result.size()) + " group" +((result.size()==0)?" ":"s ") + "in your contact book");
			}			
		};
		contactBook.getGroupService().getGroups(contactBook.getCurrentSession().getLogin(), contactBook.getCurrentSession().getPassword(), callbackGroups);
		
		// Contacts
		AsyncCallback<Set<ContactDTO>> callback = new AsyncCallback<Set<ContactDTO>>() {

			@Override
			public void onFailure(Throwable arg0) {				
			}

			@Override
			public void onSuccess(Set<ContactDTO> result) {
				labelNbContacts.setText("You have " + String.valueOf(result.size()) + " contact" +((result.size()==0)?" ":"s ") + "in your contact book");
			}			
		};		
		contactBook.getContactService().getContacts(contactBook.getCurrentSession().getLogin(), contactBook.getCurrentSession().getPassword(), callback);
	}
	
	private void clearContent() {
		if (content != null) {
			panelWidgets.clear();
			content.clear();
		}
	}
}
